package vip.xiaonuo.smzq.modular.lifecycle.controller.entity;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
import java.util.Date;
import org.springframework.web.bind.annotation.RestController;
import vip.xiaonuo.smzq.modular.lifecycle.constant.WebResultStatusConstant;
import vip.xiaonuo.smzq.modular.lifecycle.entity.LifeCyclePedigee;
import vip.xiaonuo.smzq.modular.lifecycle.mapper.entity.LifeCyclePedigeeMapper;
import vip.xiaonuo.smzq.modular.lifecycle.result.ResultWrapper;
import vip.xiaonuo.smzq.modular.lifecycle.result.WebResult;
import vip.xiaonuo.smzq.modular.lifecycle.service.entity.LifeCyclePedigeeService;

/**
 * <p>
 * 全生命周期族谱 前端控制器
 * </p>
 *
 * @author 于彦飞
 * @since 2024-04-09
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@Api(tags = {"全生命周期族谱模块"})
@RestController
@RequestMapping("/lifeCyclePedigee" )

public class LifeCyclePedigeeController {
    @Autowired
    private LifeCyclePedigeeService lifeCyclePedigeeService;
    @Autowired
    private LifeCyclePedigeeMapper lifeCyclePedigeeMapper;
    @Resource
    ResultWrapper resultWrapper;

    @ApiOperation(value = "查询所有全生命周期族谱接口", notes = "查询所有全生命周期族谱接口")
    @PostMapping(value = "/list")
    public WebResult queryLifeCyclePedigeeList(@RequestBody LifeCyclePedigee lifeCyclePedigee) {

        return resultWrapper.getSuccessWebResult(lifeCyclePedigeeService.queryLifeCyclePedigeeList(lifeCyclePedigee));
    }

    @ApiOperation(value = "分页查询全生命周期族谱接口", notes = "分页查询全生命周期族谱接口")
    @PostMapping(value = "/list/page")
    public WebResult queryLifeCyclePedigeeListPage(@RequestBody LifeCyclePedigee lifeCyclePedigee) {

        return resultWrapper.getSuccessWebResult(lifeCyclePedigeeService.queryLifeCyclePedigeeListPage(lifeCyclePedigee));
    }





    @ApiOperation(value = "根据id查询全生命周期族谱接口", notes = "根据id查询全生命周期族谱接口")
    @GetMapping(value = "byId/{id}")
    public WebResult queryLifeCyclePedigeeById(@PathVariable("id") String id) {

        LifeCyclePedigee entity  = lifeCyclePedigeeService.getById(id);
//        DictConvertUtil.convertToDictionary(entity,null);

        return resultWrapper.getSuccessWebResult(entity);
    }

    @ApiOperation(value = "新增全生命周期族谱接口", notes = "新增全生命周期族谱接口")
    @PostMapping(value = "/save")
    public WebResult save(@RequestBody LifeCyclePedigee lifeCyclePedigee) {
        boolean isSuccess = lifeCyclePedigeeService.save(lifeCyclePedigee);
        if(isSuccess){
            return resultWrapper.getWebResultByWebResultStatusConstant(WebResultStatusConstant.SUCCESS_OPERATION, lifeCyclePedigee.getId());
        }
        return resultWrapper.getWebResultByWebResultStatusConstant(WebResultStatusConstant.PROCESS_CREATE, null);
    }

    @ApiOperation(value = "批量新增全生命周期族谱接口", notes = "批量全生命周期族谱接口")
    @PostMapping(value = "save/batch")
    public WebResult saveBath(@RequestBody List<LifeCyclePedigee> lifeCyclePedigees) {
        boolean isSuccess = lifeCyclePedigeeService.saveBatch(lifeCyclePedigees);
        if(isSuccess){
            List<String> ids = lifeCyclePedigees.stream().map(ele->ele.getId()).collect(Collectors.toList());
            return resultWrapper.getWebResultByWebResultStatusConstant(WebResultStatusConstant.SUCCESS_OPERATION, ids);
        }
        return resultWrapper.getWebResultByWebResultStatusConstant(WebResultStatusConstant.PROCESS_CREATE, null);
    }

    @ApiOperation(value = "更新用户接口", notes = "id必须有值")
    @PostMapping(value = "/update")
    public WebResult update(@RequestBody LifeCyclePedigee lifeCyclePedigee) {
        return resultWrapper.getWebResultByEditSuccess(lifeCyclePedigeeService.updateById(lifeCyclePedigee));
    }
    @ApiOperation(value = "批量更新全生命周期族谱接口", notes = "id必须有值")
    @PostMapping(value = "/update/batch")
    public WebResult updateBatch(@RequestBody List<LifeCyclePedigee> lifeCyclePedigees) {
        return resultWrapper.getWebResultByCreateSuccess(lifeCyclePedigeeService.updateBatchById(lifeCyclePedigees));
    }
    @ApiOperation(value = "新增或者更新接口", notes = "id无值为新增反之则为更新")
    @PostMapping(value = "/saveOrUpdate")
    public WebResult saveOrUpdate(@RequestBody LifeCyclePedigee lifeCyclePedigee) {

        return resultWrapper.getSuccessWebResult(lifeCyclePedigeeService.saveOrUpdate(lifeCyclePedigee));
    }
    @ApiOperation(value = "批量新增或者更新接口", notes = "id无值为新增反之则为更新")
    @PostMapping(value = "/saveOrUpdate/batch")
    public WebResult saveOrUpdate(@RequestBody List<LifeCyclePedigee> lifeCyclePedigees) {

        return resultWrapper.getSuccessWebResult(lifeCyclePedigeeService.saveOrUpdateBatch(lifeCyclePedigees));
    }

}
